/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iapractica.views;

import genetics.individuos.Individuo;
import genetics.individuos.IndividuoSobrComparator;
import iapractica.controllers.PDFDesigner;
import iapractica.controllers.ReviewPastController;
import java.util.Collections;
import java.util.LinkedList;

/**
 *
 * @author Justo Vargas
 */
public class ReviewPastTestsView extends GenericView{
    private LinkedList<Integer> materiasPrimas;
    private LinkedList<Individuo> myFinalPopulation;
    private IndividualResult myWindow;

    /**
     * Creates new form ReviewPastTestsView
     */
    public ReviewPastTestsView() {
        initComponents();
        
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(iapractica.IAPracticaApp.class).getContext().getResourceMap(ReviewPastTestsView.class);
        this.setIconImage(resourceMap.getImageIcon("image.icon").getImage());
        
         Object[][] objectToModel = new Object[1][8];

        for (int i = 0; i < 1; i++) {
            Object[] filas = objectToModel[i];
            filas[0] = i + 1;
            filas[1] = 0;
            filas[2] = 0;
            filas[3] = 0;
            filas[4] = 0;
            filas[5] = 0;
            filas[6] = 0;
            filas[7] = 0;
        }

        myTable.setModel(new javax.swing.table.DefaultTableModel(
                objectToModel,
                new String[]{
                    "Solucion Nro", "Valor Aptitud", "Ganancia","Materia Prima Sobrante", "Productos 1", "Productos 2", "Productos 3", "Productos 4"
                }));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cantInd = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        myTable = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; //Disallow the editing of any cell
            }
        };
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(iapractica.IAPracticaApp.class).getContext().getResourceMap(ReviewPastTestsView.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        getContentPane().setLayout(new java.awt.GridLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        cantInd.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cantInd.setName("cantInd"); // NOI18N
        cantInd.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cantIndItemStateChanged(evt);
            }
        });
        cantInd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cantIndActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        myTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"sr", "fv", null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Valor Aptitud", "Profit", "m1", "Title 4", "Title 5", "Title 6", "Title 7", "Title 8", "Title 9", "Title 10"
            }
        ));
        myTable.setName("myTable"); // NOI18N
        myTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                myTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(myTable);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cantInd, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cantInd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                .addContainerGap())
        );

        getContentPane().add(jPanel1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void setData(int cantidadIndividuos){
        String[] myComboElements = new String[cantidadIndividuos];
        for (int i = 0; i < cantidadIndividuos; i++) {
            myComboElements[i] = "Poblacion "+(i+1);
        }
        cantInd.setModel(new javax.swing.DefaultComboBoxModel(myComboElements));
    }
    
    public void populateDataModel(LinkedList<Individuo> myFinalPopulation,  LinkedList<Integer> materiasPrimas){
        Collections.sort(myFinalPopulation, new IndividuoSobrComparator(true));

        this.myFinalPopulation = myFinalPopulation;
        
        Object[][] objectToModel = new Object[myFinalPopulation.size()][8];

        for (int i = 0; i < myFinalPopulation.size(); i++) {
            Object[] filas = objectToModel[i];
            filas[0] = i + 1;
            Individuo cromosoma = myFinalPopulation.get(i);
            filas[1] = cromosoma.getFitnessValue();
            filas[2] = cromosoma.getProfit();
            filas[3] = cromosoma.getMateriaPrimaSobrante();
            filas[4] = cromosoma.getProductsSize(1);
            filas[5] = cromosoma.getProductsSize(2);
            filas[6] = cromosoma.getProductsSize(3);
            filas[7] = cromosoma.getProductsSize(4);
        }

        myTable.setModel(new javax.swing.table.DefaultTableModel(
                objectToModel,
                new String[]{
                    "Solucion Nro", "Valor Aptitud", "Ganancia","Materia Prima Sobrante", "Productos 1", "Productos 2", "Productos 3", "Productos 4"
                }));
        
       this.materiasPrimas = materiasPrimas;
    }
    private void myTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_myTableMouseClicked
        // TODO add your handling code here:
        int rowNumber = myTable.getSelectedRow();
        //if (rowNumber > 0){
            if (myWindow == null || !myWindow.isVisible()){
                myWindow = null;
                myWindow = new IndividualResult(String.valueOf(rowNumber), myFinalPopulation.get(rowNumber), materiasPrimas, PDFDesigner.PDF_TYPES.H);
            }
            myWindow.setVisible(true);
            myWindow.setFocusable(true);
            //PopUpFactory.showConfirmPopUP(this, "Solucion Nro" + ( rowNumber + 1 ) + " \n " + myFinalPopulation.get(rowNumber));
            //}
    }//GEN-LAST:event_myTableMouseClicked

    private void cantIndItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cantIndItemStateChanged
        // TODO add your handling code here:
       
    }//GEN-LAST:event_cantIndItemStateChanged

    private void cantIndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cantIndActionPerformed
        // TODO add your handling code here:
         ReviewPastController myController = (ReviewPastController) this.getController();
        myController.notifySelection(this.cantInd.getSelectedIndex()+1);
    }//GEN-LAST:event_cantIndActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cantInd;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable myTable;
    // End of variables declaration//GEN-END:variables

    @Override
    public void reset() {
       
    }
}
